package com.yahoo.music.services
{
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import com.yahoo.music.authentication.KeyAPIManager;
	import mx.rpc.http.mxml.HTTPService;
	
	public class ArtistServices
	{
		private var uri:String= "http://us.music.yahooapis.com/artist/v1/list/search/artist/";
		private var idURI:String = "http://us.music.yahooapis.com/artist/v1/item/";
		private var _name:String;
	
		[Bindable] private var services:HTTPService;
		[Bindable] private var releaseServices:HTTPService;
		[Bindable] public  var searchResult:XML;
		[Bindable] public var  artistReleases:XML;
		[Bindable]private var _responseType:String;
			
		public function get responseType():String{
			return _responseType;
		}
		public function set responseType(value:String):void{
				_responseType = value;
		}
		public function ArtistServices(){
			super();
		}
		
		/**
		 * Search by Given Artist Name
		 * @param - Name:String
		 * @type:void
		 * */
		public function SearchArtist(name:String=''):void {
				_name = name;
				services = new HTTPService();
				services.useProxy = false;
				services.url = uri + escape(_name)
				services.resultFormat = "e4x";
				services.addEventListener(ResultEvent.RESULT,SearchResult);
				services.addEventListener(FaultEvent.FAULT,GenericFault);
				var appid:String;
				var response:String;
				var format:String;
				if(_responseType != ''){
				services.send({appid:KeyAPIManager.APP_ID,response:responseType='releases',format:"xml"});
				}
				else{
					services.send({appid:KeyAPIManager.APP_ID,format:"xml"});
				}	
		}
	
		/**
		 * Get Artits's by ID
		 * @param - artistID:String
		 * @type - void
		 **/ 
		public function getArtistReleases(artistID:String):void {
			
				releaseServices = new HTTPService();
				releaseServices.url = idURI+escape(artistID);
				releaseServices.useProxy = false;
				releaseServices.resultFormat="e4x";
				releaseServices.addEventListener(ResultEvent.RESULT,ReleasesResult);
				releaseServices.addEventListener(FaultEvent.FAULT,GenericFault);
				var response:String;
				var appid:String;
				releaseServices.send({appid:KeyAPIManager.APP_ID,response:"releases",format:"xml"});
		}

		private function ReleasesResult(e:ResultEvent):void{
					artistReleases = e.result as XML;
		}
		private function SearchResult(e:ResultEvent):void{
					searchResult = e.result as XML;
		}
		private function GenericFault():void {
				Alert.show('Error');
		}
		public function get ArtistName():String{
			return _name;
		}
		public function set ArtistName(value:String):void{
			_name = value;
		}
	}
}